﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;

using PriceLibrary.PriceService;

using VDataAdapter.QueueManagement;
using VDataAdapter.EventManagement;

namespace VDataAdapter.PriceServerAdapter
{
    public partial class InputEventForm : Form
    {
        private IEventStoteredQueue _eventStorer;

        private Event[] _events;

        public InputEventForm(IEventStoteredQueue eventStorer)
        {
            _eventStorer = eventStorer;
            SendBeginEnRecovery();
            InitializeComponent();
        }

        Dictionary<int, MarketInfosEvent> _sampleMarketEvent = new Dictionary<int, MarketInfosEvent>();

        private Dictionary<int, MarketInfosEvent> GetSampleMarketEvent(MarketInfosEvent[] marketEvents)
        {
            var floorCode = new List<int>();

            var result = new Dictionary<int, MarketInfosEvent>();

            foreach (MarketInfosEvent e in marketEvents)
            {
                foreach (KeyValuePair<int, MarketInfo> market in e.MarketInfosList)
                {
                    if (!floorCode.Contains(market.Key))
                    {
                        floorCode.Add(market.Key);
                        var marketInfosList = new Dictionary<int, MarketInfo>();
                        marketInfosList.Add(market.Key, market.Value);
                        var marketEvent = new MarketInfosEvent(marketInfosList);
                        result.Add(market.Key, marketEvent);
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// this is to send all beginning message so that I can freely send msg to VData
        /// </summary>
        private void SendBeginEnRecovery()
        {
            FirstTimeEvent firstTimeEvent = TestDataUtils.LoadFirstTimeEvent();

            TransactionsEvent[] transactionEvents = TestDataUtils.LoadTransactionEventList();

            MarketInfosEvent[] marketEvents = TestDataUtils.LoadMarketInfoEvent();

            _sampleMarketEvent = GetSampleMarketEvent(marketEvents);

            _eventStorer.PutFirstTimeEvent(firstTimeEvent);
            
            foreach(TransactionsEvent e in transactionEvents)
            {
                _eventStorer.PutTransactionsEvent(e);
            }
        }

        private MarketInfo Clone(MarketInfo input)
        {
            MarketInfo result = new MarketInfo();
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(PriceLibrary.PriceService.MarketInfo));
            foreach (PropertyDescriptor prop in properties)
            {
                prop.SetValue(result, prop.GetValue(input));
            }
            return result;
        }

        private void marketInfoBtn_Click(object sender, EventArgs e)
        {
            int floorCode = int.Parse(txtBoxFloor.Text);

            Dictionary<int, MarketInfo> d = new Dictionary<int, MarketInfo>();
            
            MarketInfo market = Clone(_sampleMarketEvent[floorCode].MarketInfosList[floorCode]);
            market.TotalShares = double.Parse(txtBoxVolume.Text);
            market.MarketIndex = double.Parse(txtBoxValue.Text);
            DateTime date = DateTime.Now;
             
            market.TradingTime = String.Format("{0}{1}{2}", date.Hour - 8, date.Minute, date.Second); 
            d.Add(floorCode, market);

            MarketInfosEvent sample = new MarketInfosEvent(d);

            _eventStorer.PutMarketInfosEvent(sample);
        }
    }
}
